<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>卡片名称</span>
        <el-button type="primary" @click="addDialog = true">添加分类</el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="list" style="width: 100%" stripe border>
        <el-table-column prop="id" label="ID" width="180"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名" width="180"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="obj">
            <el-button @click="editCategory(obj.row)" type="text">编辑</el-button>
            <el-button @click="del(obj.row.id)" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- dialog弹框 ---添加-->
    <el-dialog title="添加分类" :visible.sync="addDialog" width="500px" :before-close="cancel">
      <el-form :model="addData" :rules="rules" ref="addForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addData.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addData.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </div>
    </el-dialog>
    <!-- dialog弹框 ---编辑-->
    <el-dialog title="编辑" :visible.sync="editDialog" width="500px" :before-close="editcancel">
      <el-form :model="editdata" :rules="rules" ref="editForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editdata.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editdata.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editcancel">取 消</el-button>
        <el-button type="primary" @click="updateCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getCategoryAPI, addCategoryAPI, delCategoryAPI, updateCategoryAPI } from '@/api/category'
export default {
  name: 'CateList',
  data() {
    return {
      list: [],
      addDialog: false,
      addData: {}, // 添加
      editDialog: false, // 编辑
      editdata: {}, // 编辑数据
      // 验证的对象
      rules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { min: 1, max: 10, message: '长度1~10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          // {1,15} 不能写成 {1, 15}
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '只能是长度1~15位的字母数字组合',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 获取分类
    async getCategory() {
      const { data: res } = await getCategoryAPI()
      if (res.code === 0) {
        this.list = res.data
      }
    },
    // 添加分类
    addCategory() {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCategoryAPI(this.addData)
        if (res.code === 0) {
          this.$message.success(res.message) // 提示
          this.$refs.addForm.resetFields() // 重置表单
          this.addDialog = false // 关闭弹框
          this.getCategory() // 重新获取分类列表数据
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 点击取消按钮
    cancel() {
      this.$refs.addForm.resetFields() // 重置表单
      this.addDialog = false // 关闭添加弹框
    },
    // 取消编辑事件
    editcancel() {
      this.$refs.editForm.resetFields() // 重置表单
      this.editDialog = false // 关闭添加弹框
    },
    // 删除分类
    del(id) {
      /*  this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await delCategoryAPI(id)
        console.log(res)
        this.$message.success(res.message)
        this.getCategory() // 重新获取
      }) */

      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning',
        callback: async r => {
          if (r === 'confirm') {
            const { data: res } = await delCategoryAPI(id)
            console.log(res)
            this.$message.success(res.message)
            this.getCategory() // 重新获取
          }
        }
      })
    },
    // 编辑数据
    editCategory(row) {
      this.editDialog = true
      console.log(row)
      this.editdata = { ...row }
    },
    // 更新数据
    updateCategory() {
      console.log(222)
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateCategoryAPI(this.editdata)
        if (res.code === 0) {
          this.getCategory() // 重新获取
          this.$refs.editForm.resetFields() // 清空表单
          this.editDialog = false // 关闭编辑弹框
          this.$message.success(res.message)
        }
      })
    }
  },
  created() {
    this.getCategory()
  },
  mounted() {
    console.log(this)
  }
}
</script>
<style lang="less" scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
